package com.flurry.sdk;

import com.flurry.sdk.ip;
import com.flurry.sdk.ks;
import com.flurry.sdk.ku;
import io.fabric.sdk.android.services.network.HttpRequest;
import java.net.SocketTimeoutException;
import java.util.List;
import java.util.Map;

public class io extends kx<ip> {
    public static long a;
    /* access modifiers changed from: private */
    public static final String e = io.class.getSimpleName();

    /* access modifiers changed from: protected */
    public final /* synthetic */ void a(kw kwVar) {
        final ip ipVar = (ip) kwVar;
        km.a(3, e, "Sending next pulse report to " + ipVar.k + " at: " + ipVar.r);
        jk.a();
        long c = jk.c();
        if (c == 0) {
            c = a;
        }
        jk.a();
        long f = jk.f();
        if (f == 0) {
            f = System.currentTimeMillis() - c;
        }
        final iq iqVar = new iq(ipVar, c, f, ipVar.p);
        ks ksVar = new ks();
        ksVar.f = ipVar.r;
        ksVar.w = 100000;
        if (ipVar.e.equals(iw.POST)) {
            ksVar.c = new lc();
            if (ipVar.j != null) {
                ksVar.b = ipVar.j.getBytes();
            }
            ksVar.g = ku.a.kPost;
        } else {
            ksVar.g = ku.a.kGet;
        }
        ksVar.h = ipVar.h * 1000;
        ksVar.i = ipVar.i * 1000;
        ksVar.l = true;
        ksVar.r = true;
        ksVar.s = (ipVar.h + ipVar.i) * 1000;
        Map<String, String> map = ipVar.f;
        if (map != null) {
            for (String next : ipVar.f.keySet()) {
                ksVar.a(next, map.get(next));
            }
        }
        ksVar.j = false;
        ksVar.a = new ks.a<byte[], String>() {
            public final /* synthetic */ void a(ks ksVar, Object obj) {
                boolean z;
                boolean z2 = false;
                String str = (String) obj;
                km.a(3, io.e, "Pulse report to " + ipVar.k + " for " + ipVar.m.c + ", HTTP status code is: " + ksVar.p);
                int i = ksVar.p;
                iq iqVar = iqVar;
                int i2 = (int) ksVar.n;
                if (i2 >= 0) {
                    iqVar.k = ((long) i2) + iqVar.k;
                } else if (iqVar.k <= 0) {
                    iqVar.k = 0;
                }
                iqVar.e = i;
                if (!ksVar.c()) {
                    Exception exc = ksVar.o;
                    if (ksVar.o == null || !(ksVar.o instanceof SocketTimeoutException)) {
                        z = false;
                    } else {
                        z = true;
                    }
                    if (ksVar.t || z) {
                        z2 = true;
                    }
                    if (z2) {
                        if (ksVar.e()) {
                            km.a(3, io.e, "Timeout occured when trying to connect to: " + ipVar.k + ". Exception: " + ksVar.o.getMessage());
                        } else {
                            km.a(3, io.e, "Manually managed http request timeout occured for: " + ipVar.k);
                        }
                        io.a(io.this, iqVar, ipVar);
                        return;
                    }
                    km.a(3, io.e, "Error occured when trying to connect to: " + ipVar.k + ". Exception: " + exc.getMessage());
                    io.a(io.this, iqVar, ipVar, str);
                } else if (i >= 200 && i < 300) {
                    io.b(io.this, iqVar, ipVar);
                } else if (i < 300 || i >= 400) {
                    km.a(3, io.e, ipVar.m.c + " report failed sending to : " + ipVar.k);
                    io.a(io.this, iqVar, ipVar, str);
                } else {
                    io.a(io.this, iqVar, ipVar, ksVar);
                }
            }
        };
        jw.a().a((Object) this, ksVar);
    }

    public io() {
        kx.b = 30000;
        this.d = kx.b;
    }

    /* access modifiers changed from: protected */
    public final kf<List<ip>> a() {
        return new kf<>(jy.a().a.getFileStreamPath(".yflurryanpulsecallbackreporter"), ".yflurryanpulsecallbackreporter", 2, new lj<List<ip>>() {
            public final lg<List<ip>> a(int i) {
                return new lf(new ip.a());
            }
        });
    }

    /* access modifiers changed from: protected */
    public final synchronized void a(List<ip> list) {
        is.a().c();
    }

    /* access modifiers changed from: protected */
    public final synchronized void b(List<ip> list) {
        is.a();
        List<it> d = is.d();
        if (d != null) {
            if (d.size() != 0) {
                km.a(3, e, "Restoring " + d.size() + " from report queue.");
                for (it b : d) {
                    is.a().b(b);
                }
                is.a();
                for (it a2 : is.b()) {
                    for (ip next : a2.a()) {
                        if (!next.l) {
                            km.a(3, e, "Callback for " + next.m.c + " to " + next.k + " not completed.  Adding to reporter queue.");
                            list.add(next);
                        }
                    }
                }
            }
        }
    }

    static /* synthetic */ void a(io ioVar, iq iqVar, ip ipVar) {
        is.a().b(iqVar);
        ioVar.c(ipVar);
    }

    static /* synthetic */ void a(io ioVar, iq iqVar, ip ipVar, String str) {
        boolean b = is.a().b(iqVar, str);
        km.a(3, e, "Failed report retrying: " + b);
        if (b) {
            ioVar.d(ipVar);
        } else {
            ioVar.c(ipVar);
        }
    }

    static /* synthetic */ void b(io ioVar, iq iqVar, ip ipVar) {
        km.a(3, e, ipVar.m.c + " report sent successfully to : " + ipVar.k);
        is.a().a(iqVar);
        ioVar.c(ipVar);
    }

    static /* synthetic */ void a(io ioVar, iq iqVar, ip ipVar, ks ksVar) {
        String str = null;
        List<String> a2 = ksVar.a(HttpRequest.HEADER_LOCATION);
        if (a2 != null && a2.size() > 0) {
            str = mc.b(a2.get(0), ipVar.q);
        }
        boolean a3 = is.a().a(iqVar, str);
        if (a3) {
            km.a(3, e, "Received redirect url. Retrying: " + str);
        } else {
            km.a(3, e, "Received redirect url. Retrying: false");
        }
        if (a3) {
            ipVar.r = str;
            ksVar.f = str;
            if (ksVar.q != null && ksVar.q.a.containsKey(HttpRequest.HEADER_LOCATION)) {
                ksVar.q.b(HttpRequest.HEADER_LOCATION);
            }
            jw.a().a((Object) ioVar, ksVar);
            return;
        }
        ioVar.c(ipVar);
    }
}
